﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using Tesla.Content;
using Tesla.Math;

#pragma warning disable 1591

namespace Tesla.Bounding {
    public sealed class OrientedBoundingBox : BoundingVolume {
        private Vector3 m_xAxis;
        private Vector3 m_yAxis;
        private Vector3 m_zAxis;
        private Vector3 m_extents;
        private bool m_correctCorners;
        private Vector3[] m_corners;

        public Vector3 XAxis {
            get {
                return m_xAxis;
            }
            set {
                m_xAxis = value;
                m_correctCorners = false;
            }
        }

        public Vector3 YAxis {
            get {
                return m_yAxis;
            }
            set {
                m_yAxis = value;
                m_correctCorners = false;
            }
        }

        public Vector3 ZAxis {
            get {
                return m_zAxis;
            }
            set {
                m_zAxis = value;
                m_correctCorners = false;
            }
        }

        public Vector3 Extents {
            get {
                return m_extents;
            }
            set {
                m_extents = value;
                m_correctCorners = false;
            }
        }

        public bool CorrectCorners {
            get {
                return m_correctCorners;
            }
        }

        public Vector3[] Corners {
            get {
                return m_corners;
            }
        }

        public override float Volume {
            get { 
                return 8 * ((m_extents.X * m_extents.Y * m_extents.Z) * 2); 
            }
        }

        public override BoundingType BoundingType {
            get {
                return BoundingType.OBB;
            }
        }

        public OrientedBoundingBox()
            : base() {
                m_center = Vector3.Zero;
                m_xAxis = Vector3.UnitX;
                m_yAxis = Vector3.UnitY;
                m_zAxis = Vector3.UnitZ;
                m_extents = Vector3.Zero;
                m_correctCorners = false;
                m_corners = new Vector3[8];
        }

        public OrientedBoundingBox(Vector3 center, Vector3 xAxis, Vector3 yAxis, Vector3 zAxis, Vector3 extents)
            : base(center) {
                m_xAxis = xAxis;
                m_yAxis = yAxis;
                m_zAxis = zAxis;
                m_extents = extents;
                m_correctCorners = false;
                m_corners = new Vector3[8];
        }

        public OrientedBoundingBox(OrientedBoundingBox source) {
            m_center = source.Center;
            m_xAxis = source.XAxis;
            m_yAxis = source.YAxis;
            m_zAxis = source.ZAxis;
            m_extents = source.Extents;
            m_correctCorners = source.CorrectCorners;
            m_corners = new Vector3[8];
            Vector3[] srcCorners = source.Corners;
            for(int i = 0; i < 8; i++) {
                m_corners[i] = srcCorners[i];
            }
        }

        public void ComputeCorners() {
            if(m_correctCorners) {
                return;
            }

            Vector3 exAxis;
            Vector3 eyAxis;
            Vector3 ezAxis;

            Vector3.Multiply(ref m_xAxis, m_extents.X, out exAxis);
            Vector3.Multiply(ref m_yAxis, m_extents.Y, out eyAxis);
            Vector3.Multiply(ref m_zAxis, m_extents.Z, out ezAxis);

            Vector3 temp;

            Vector3.Subtract(ref m_center, ref exAxis, out temp);
            Vector3.Subtract(ref temp, ref eyAxis, out temp);
            Vector3.Subtract(ref temp, ref ezAxis, out temp);
            m_corners[0] = temp;

            Vector3.Add(ref m_center, ref exAxis, out temp);
            Vector3.Subtract(ref temp, ref eyAxis, out temp);
            Vector3.Subtract(ref temp, ref ezAxis, out temp);
            m_corners[1] = temp;

            Vector3.Add(ref m_center, ref exAxis, out temp);
            Vector3.Add(ref temp, ref eyAxis, out temp);
            Vector3.Subtract(ref temp, ref ezAxis, out temp);
            m_corners[2] = temp;

            Vector3.Subtract(ref m_center, ref exAxis, out temp);
            Vector3.Add(ref temp, ref eyAxis, out temp);
            Vector3.Subtract(ref temp, ref ezAxis, out temp);
            m_corners[3] = temp;

            Vector3.Add(ref m_center, ref exAxis, out temp);
            Vector3.Subtract(ref temp, ref eyAxis, out temp);
            Vector3.Add(ref temp, ref ezAxis, out temp);
            m_corners[4] = temp;

            Vector3.Subtract(ref m_center, ref exAxis, out temp);
            Vector3.Subtract(ref temp, ref eyAxis, out temp);
            Vector3.Add(ref temp, ref ezAxis, out temp);
            m_corners[5] = temp;

            Vector3.Add(ref m_center, ref exAxis, out temp);
            Vector3.Add(ref temp, ref eyAxis, out temp);
            Vector3.Add(ref temp, ref ezAxis, out temp);
            m_corners[6] = temp;

            Vector3.Subtract(ref m_center, ref exAxis, out temp);
            Vector3.Add(ref temp, ref eyAxis, out temp);
            Vector3.Add(ref temp, ref ezAxis, out temp);
            m_corners[7] = temp;


            m_correctCorners = true;
        }

        public override void Set(BoundingVolume volume) {
            throw new System.NotImplementedException();
        }

        public void Set(Vector3 center, Vector3 xAxis, Vector3 yAxis, Vector3 zAxis, Vector3 extents) {
            throw new System.NotImplementedException();
        }

        public override BoundingVolume Clone() {
            return new OrientedBoundingBox(this);
        }

        public override float DistanceToEdge(Vector3 point) {
            throw new System.NotImplementedException();
        }

        public override void ComputeFromPoints(Vector3[] points) {
            throw new System.NotImplementedException();
        }

        public override void ComputeFromPrimitives(Vector3[] vertices, int[] indices) {
            throw new System.NotImplementedException();
        }

        public override void ComputeFromPrimitives(Vector3[] vertices, short[] indices) {
            throw new System.NotImplementedException();
        }

        public override bool Intersects(BoundingBox box) {
            throw new System.NotImplementedException();
        }

        public override bool Intersects(BoundingSphere sphere) {
            throw new System.NotImplementedException();
        }

        public override bool Intersects(OrientedBoundingBox obb) {
            throw new System.NotImplementedException();
        }

        public override bool Intersects(Ray ray) {
            throw new System.NotImplementedException();
        }

        public override void Intersects(ref Ray ray, out bool result) {
            throw new System.NotImplementedException();
        }

        public override BoundingIntersectionRecord IntersectsWhere(Ray ray) {
            throw new System.NotImplementedException();
        }

        public override void IntersectsWhere(ref Ray ray, out BoundingIntersectionRecord result) {
            throw new System.NotImplementedException();
        }

        public override PlaneIntersectionType Intersects(Plane plane) {
            throw new System.NotImplementedException();
        }

        public override void Intersects(ref Plane plane, out PlaneIntersectionType result) {
            throw new System.NotImplementedException();
        }

        public override ContainmentType Contains(Vector3 point) {
            throw new System.NotImplementedException();
        }

        public override ContainmentType Contains(BoundingBox box) {
            throw new System.NotImplementedException();
        }

        public override ContainmentType Contains(BoundingSphere sphere) {
            throw new System.NotImplementedException();
        }

        public override ContainmentType Contains(OrientedBoundingBox obb) {
            throw new System.NotImplementedException();
        }

        public override void MergeLocal(BoundingVolume volume) {
            throw new System.NotImplementedException();
        }

        public override BoundingVolume Merge(BoundingVolume volume) {
            throw new System.NotImplementedException();
        }

        public override void TransformLocal(Transform transform) {
            throw new System.NotImplementedException();
        }

        public override BoundingVolume Transform(Transform transform) {
            throw new System.NotImplementedException();
        }

        /// <summary>
        /// Serializes this OrientedBoundingBox.
        /// </summary>
        /// <param name="output">Output to write to</param>
        public override void Write(ISavableWriter output) {
            base.Write(output);
            output.Write("Extents", m_extents);
            output.Write("XAxis", m_xAxis);
            output.Write("YAxis", m_yAxis);
            output.Write("ZAxis", m_zAxis);
        }

        /// <summary>
        /// Deserializes this OrientedBoundingBox.
        /// </summary>
        /// <param name="input">Input to read from</param>
        public override void Read(ISavableReader input) {
            base.Read(input);
            m_extents = input.ReadVector3();
            m_xAxis = input.ReadVector3();
            m_yAxis = input.ReadVector3();
            m_zAxis = input.ReadVector3();
        }
    }
}
